package com.boot2.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.boot2.core.constant.PayEnum;
import com.boot2.core.exception.BusinessException;
import lombok.Data;
import org.springframework.util.Assert;

import java.math.BigDecimal;

/**
 * @author zhangweilin
 * @Description: 货币汇率查询工具
 * @date 2019/5/25
 */
public class CurrencyRateUtil {

    /**
     * 请求路径  如 http://apicloud.mob.com/exchange/code/query?key=xxxx&code=CNYUSD
     * 即人民币对美元汇率
     */
    private static final String baseUrl = "http://apicloud.mob.com/exchange/code/query?key=%s&code=%s%s";

    /**
     * 请求接口获取数据
     *
     * @param apiKey
     * @param fromCurrency
     * @param toCurrency
     * @return
     */
    public static String get(String apiKey, String fromCurrency, String toCurrency) {
        String url = String.format(baseUrl, apiKey, fromCurrency, toCurrency);
        String result = HttpsClient.get(url, null);
        return result;
    }

    /**
     * 获取对应的价格
     *
     * @param apiKey
     * @param fromCurrency
     * @param toCurrency
     * @return
     */
    public static BigDecimal getCurrencyRate(String apiKey, BigDecimal amount, String fromCurrency, String toCurrency) {
        String result = get(apiKey, fromCurrency, toCurrency);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String retCode = jsonObject.getString("retCode");
        //成功
        if ("200".equalsIgnoreCase(retCode)) {
            JSONObject jsonObject1 = jsonObject.getJSONObject("result");
            String sellPic = jsonObject1.getString("sellPic");
//            return new BigDecimal(sellPic).multiply(amount);
            return new BigDecimal(sellPic);
        } else {
            String msg = jsonObject.getString("msg");
            throw new BusinessException("获取货币汇率失败,msg: " + msg + " , amount: " + amount + " ,fromCurrency: " + fromCurrency + " ,toCurrency: " + toCurrency);
        }
    }

    /**
     * 获取指定汇率
     *
     * @param apiKey
     * @param amount
     * @param fromCurrency
     * @param toCurrency
     * @return
     */
    public static Amount getAmount(String apiKey, BigDecimal amount, String fromCurrency, String toCurrency) {
        BigDecimal currencyRate = getCurrencyRate(apiKey, amount, fromCurrency, toCurrency);
        BigDecimal amountValue = currencyRate.multiply(amount);
        String amountFormula=amount+"("+fromCurrency+")×"+currencyRate;
        Amount amount1 = new Amount(fromCurrency, toCurrency, amountValue,currencyRate,amountFormula);
        return amount1;
    }

    /**
     * 获取对应的人民币金额
     *
     * @param apiKey
     * @param amount
     * @param fromCurrency 来源货币
     * @return
     */
    public static Amount getCNYAmount(String apiKey, BigDecimal amount, String fromCurrency) {
        if (PayEnum.Currency.CNY.name().equalsIgnoreCase(fromCurrency)) {
            return new Amount(fromCurrency, fromCurrency, amount,new BigDecimal(1),null);
        }
        return getAmount(apiKey, amount, fromCurrency, PayEnum.Currency.CNY.name());
    }

    @Data
    public static class Amount {

        /**
         * 原货币
         */
        private String fromCurrency;

        /**
         * 目标货币
         */
        private String toCurrency;

        /**
         * 所使用的货币汇率
         */
        private BigDecimal currencyRate;

        /**
         * 实际使用的金额数量
         */
        private BigDecimal amount;

        /**
         * 所使用的计算公式
         */
        private String amountFormula;

        /**
         * 是否兑换过，如果原货币和目标货币一致，则为未兑换过
         */
        private boolean isExchanged = false;

        public Amount(String fromCurrency, String toCurrency, BigDecimal amount, BigDecimal currencyRate,String amountFormula) {
            Assert.isTrue(StringUtils.isNotEmpty(fromCurrency), "源货币不能为空");
            Assert.isTrue(StringUtils.isNotEmpty(toCurrency), "目标货币不能为空");
            Assert.notNull(amount, "金额不能为空");
            Assert.notNull(currencyRate, "所使用的货币汇率不能为空");
            this.fromCurrency = fromCurrency;
            this.toCurrency = toCurrency;
            this.amount = amount;
            this.currencyRate = currencyRate;
            this.amountFormula = amountFormula;
            if (fromCurrency.equalsIgnoreCase(toCurrency)) {
                this.isExchanged = true;
            }
        }

    }

}
